<template>
  <div class="title">
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item :to="{ path: '/index/goods' }"
        >商品管理页面</el-breadcrumb-item
      >
      <el-breadcrumb-item :to="{ path: '/index/goodsadd'}">新品上线</el-breadcrumb-item>
    </el-breadcrumb>
  </div>
  <div class="add">
    <el-card class="box-card">
      <template #header>
        <div class="card-header">
          <span>新增商品数据</span>
        </div>
      </template>
      <el-form
        label-width="120px"
        :rules="newgoodslist.rules"
        ref="uploadForm"
        :model="newgoodslist"
        :size="formSize"
        status-icon
      >
        <el-form-item required label="商品分类">
          <el-cascader style="width: 300px" v-model="newgoodslist.goodsid"></el-cascader>
        </el-form-item>
        <el-form-item required label="商品名" prop="goodsName">
          <el-input
            style="width: 300px"
            placeholder="请输入商品名"
            v-model="newgoodslist.goodsName"
          />
        </el-form-item>
        <el-form-item required label="商品编号" prop="goodsid">
          <el-input
            style="width: 300px"
            placeholder="请输入商品编号"
            v-model="newgoodslist.goodsid"
          />
        </el-form-item>
        <el-form-item required label="商品价格" prop="goodsPrice">
          <el-input
            style="width: 300px"
            type="number"
            min="0"
            placeholder="请输入商品价格"
            v-model="newgoodslist.goodsPrice"
          />
        </el-form-item>
        <el-form-item required label="商品库存" prop="goodsNum">
          <el-input
            style="width: 300px"
            type="number"
            min="0"
            placeholder="请输入商品库存"
            v-model="newgoodslist.goodsNum"
          />
        </el-form-item>
        <el-form-item label="商品简介" prop="goodsDetails">
          <el-input
            type="textarea"
            style="width: 300px"
            placeholder="请输入商品简介"
            v-model="newgoodslist.goodsDetails"
          />
        </el-form-item>
        <el-form-item label="上架状态">
          <el-radio-group v-model="newgoodslist.goodsState">
            <el-radio label="0">上架</el-radio>
            <el-radio label="1">下架</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item required label="商品主图">
          <el-upload
            v-model="imageUrl"
            class="upload-demo"
            drag
            action=""
            multiple
            show-file-list
          >
            <el-icon class="el-icon--upload"><upload-filled /></el-icon>
            <div class="el-upload__text">拖拽此处上传<em>或点击上传</em></div>
            <template #tip>
              <div class="el-upload__tip">上传jpg/png 文件要小于500KB</div>
            </template>
          </el-upload>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="submitForm(uploadForm)">{{
            newgoodslist.id ? "立即修改" : "立即创建"
          }}</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script setup>
import axios from "axios";
import { ref } from "vue";
import { useRoute, useRouter } from 'vue-router'
import { goods_add} from '../../../utils/api'
const formSize = ref('default')
const router = useRouter()
const uploadForm = ref();
const newgoodslist = ref({
  goodsid: '',
  goodsName: "",
  goodsPrice: "",
  goodsNum: "",
  goodsDetails: "",
  id: "",
  goodsState: "0",
  imageUrl: "",
  rules: {
    goodsName: [
      { required: true, message: "请填写商品名称", trigger: "change"},
    ],
    goodsPrice: [
      { required: true, message: "请填写商品价格", trigger: "change"},
    ],
    goodsNum: [
      { required: true, message: "请填写商品库存", trigger: "change"},
    ],
    goodsDetails: [
      { required: true, message: "请填写商品详情", trigger: "change" },
    ],
    goodsid: [
      { required: true, message: "请填写商品编号", trigger: "change" },
    ],
  },
});
const uploadFormData = ref({ ...newgoodslist });


// 提交表单
const submitForm = async (formEl) => {
  if (!formEl) return
  await formEl.validate((valid, fields) => {
    if (valid) {
      submitAdd();
      console.log('submit!')
    } else {
      console.log('error submit!', fields)
    }
  })
}

const submitAdd = (id) => {
  if (!id) {
    //新增用 post 方法
    let params = {
      goodsid: newgoodslist.value.goodsid,
      goodsname: newgoodslist.value.goodsName,
      goodsprice: newgoodslist.value.goodsPrice,
      goodsstate: newgoodslist.value.goodsState,
      goodsnum: newgoodslist.value.goodsNum,
      goodsdetails: newgoodslist.value.goodsDetails,
      imageUrl: newgoodslist.value.imageUrl,
    };
    goods_add(params).then((res) => {
      ElMessage.success('添加成功')
      router.push({ path: '/index/goods' })
    });
  }

};
</script>

<style scoped>
.el-breadcrumb {
  margin: 10px !important;
}
</style>
